﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace HMViewerWPF
{
    /// <summary>
    /// Class containing method to facilitate Settings maangements
    /// </summary>
    public class ConfigHelper
    {
        
        private Configuration _config ;


        
        public ConfigHelper()
        {
            _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }



        
        /// <summary>
        /// Return a value from the app.setting file
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="defaultValue"></param>
        public string GetValueFromAppConfig(string keyName,string defaultValue)
        {
            string currentValue = _config.AppSettings.Settings[keyName].Value.ToString();


            if (!string.IsNullOrEmpty(currentValue))
                return _config.AppSettings.Settings[keyName].Value.ToString();
            else
                return defaultValue;
        }

        /// <summary>
        /// Set a value to the app.setting file
        /// </summary>
        /// <param name="keyName"></param>
        /// <param name="keyValue"></param>
        public void SaveValueFromAppConfig(string keyName, string keyValue)
        {



            string currentValue = _config.AppSettings.Settings[keyName].Value ;

            if (string.IsNullOrEmpty(currentValue))
            {
                _config.AppSettings.Settings.Add(keyName, keyValue);
                _config.Save();
            }
            else
                if (currentValue != keyValue)
                {
                    _config.AppSettings.Settings[keyName].Value = keyValue;
                    _config.Save();
                }


        }

    }
}
